//
//  YHYHunterCityTableViewCell.m
//  TripTogether
//
//  Created by 杨泓晔 on 15/12/22.
//  Copyright © 2015年 杨泓晔. All rights reserved.
//

#import "YHYHunterCityTableViewCell.h"
#import "YHYHunterCitySubCollectionViewCell.h"
@interface YHYHunterCityTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate>
//白底图片
@property(nonatomic,retain)UIImageView *myImageView;

//图片
@property(nonatomic,retain)UIImageView *pageImageView;/**<图片*/

@property (nonatomic,retain)UIView *userView;/**<发布者头像白底*/

//发布者头像
@property(nonatomic,retain)UIImageView *userImageView;/**<发布者头像*/

//标题
@property(nonatomic,retain)UILabel *title;/**<标题*/

//日期
@property(nonatomic,retain)UILabel *data;/**<日期*/

//地点
@property(nonatomic,retain)UILabel *address;/**<地点*/

//标签
@property(nonatomic,retain)UILabel *tab_list;/**<标签*/

//价格
@property(nonatomic,retain)UILabel *price;/**<价格*/

//喜欢人数
@property(nonatomic,retain)UILabel *like_count;/**<喜欢人数*/

@property (nonatomic,retain)UILabel *priceName;/**<¥*/

//@property (nonatomic, strong) UILabel *newLabel;
@property (nonatomic, strong) UILabel *lastLael;

@property (nonatomic,strong)NSMutableArray *myArrayOfLable;

@property (nonatomic,strong)UICollectionView *collection;
@end

@implementation YHYHunterCityTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCell];
        _myArrayOfLable = [NSMutableArray array];
    }
    return self;
}

- (void)createCell{
#pragma mark _myImageView + 布局

    _myImageView = [UIImageView new];
    _myImageView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_myImageView];
    
    [_myImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(5);
        make.right.mas_equalTo(self).offset(-5);
        make.height.mas_equalTo(360);
      }];
    //裁掉边框
    _myImageView.layer.masksToBounds = YES;
    //设置半径
    _myImageView.layer.cornerRadius = 7 * AUTO_WIDTH;
    _myImageView.contentMode = UIViewContentModeScaleAspectFill;
    
    
    
#pragma mark _pageImageView + make布局
    _pageImageView = [UIImageView new];
  //  _pageImageView.backgroundColor = [UIColor redColor];
    [_myImageView addSubview:_pageImageView];

    
    [_pageImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(_myImageView);
        make.height.mas_equalTo(_myImageView).multipliedBy(0.6);
        make.right.mas_equalTo(_myImageView).offset(0);
        }];
    
    //裁掉边框
    self.pageImageView.layer.masksToBounds = YES;
    //设置半径
    self.pageImageView.layer.cornerRadius = 5 * AUTO_WIDTH;
    
#pragma mark 发布者头像白底
    _userView = [UIView new];
    _userView.backgroundColor = [UIColor whiteColor];
    [_myImageView addSubview:_userView];
    
    [_userView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_myImageView.mas_right).offset(-90);
        make.height.and.width.mas_equalTo(@60);
        //216 = 360 * 0.6
        make.top.mas_equalTo(_pageImageView.mas_top).offset(216 - 60 /2);
        
    }];

    //设置半径
    _userView.layer.cornerRadius = 30.f;
    //裁掉边框
    _userView.layer.masksToBounds = YES;

    
#pragma mark 发布者头像_userImageView
    _userImageView = [UIImageView new];
    _userImageView.backgroundColor = [UIColor clearColor];
    [_myImageView addSubview:_userImageView];
    
    
    [_userImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_myImageView.mas_right).offset(-87);
        make.height.mas_equalTo(54);
        make.width.mas_equalTo(54);
        make.top.mas_equalTo(_pageImageView.mas_top).offset(216 - 54/2);
    }];
//    //裁掉边框
    self.userImageView.layer.masksToBounds = YES;
    //设置半径
    self.userImageView.layer.cornerRadius = 54 / 2.f ;
//
#pragma mark 标题_title
    _title = [UILabel new];

    [_myImageView addSubview:_title];
    
    [_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(_myImageView).offset(-140);
        make.top.mas_equalTo(_pageImageView.mas_bottom).offset(5);
        make.height.mas_equalTo(50);
    }];
    //字体加粗
    _title.numberOfLines = 0;
    [_title setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
    //_title.font = [UIFont fontWithName:@"Heiti TC" size:18.f];
    _title.textColor = [UIColor colorWithWhite:0.235 alpha:1.000];
    
#pragma mark 日期
    
    _data = [UILabel new];
   // _data.backgroundColor = [UIColor redColor];
    [_myImageView addSubview:_data];
    
    [_data mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_title);
        make.top.mas_equalTo(_title.mas_bottom).offset(3);
//        make.width.mas_equalTo(120);
        make.height.mas_equalTo(20);
        
    }];
    
    _data.textColor = [UIColor colorWithWhite:0.533 alpha:1.000];////
    [_data setFont:[UIFont fontWithName:@"Lao Sangam MN" size:17]];

#pragma mark 地点
    _address = [UILabel new];
    _address.backgroundColor = [UIColor clearColor];
    _address.textColor = [UIColor colorWithWhite:0.533 alpha:1.000];////
    [_address setFont:[UIFont fontWithName:@"Lao Sangam MN" size:15]];
    [_myImageView addSubview:_address];
 
    [_address mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_data.mas_right).offset(0);
        make.height.top.mas_equalTo(_data);
       // make.width.mas_equalTo(120);
        
    }];
    
    

#pragma mark 喜欢人数
    _like_count = [UILabel new];
   // _like_count.backgroundColor = [UIColor redColor];
    [_myImageView addSubview:_like_count];
    
    [_like_count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_address.mas_right).offset(0);
        make.height.top.mas_equalTo(_data);
        make.width.mas_equalTo(120);
       // make.left.mas_equalTo(_data.mas_right).offset(150).priorityLow(); // 优先级;
    }];
    
    _like_count.textColor = [UIColor colorWithWhite:0.533 alpha:1.000];////
    [_like_count setFont:[UIFont fontWithName:@"Lao Sangam MN" size:15]];
//    
    
#pragma mark _priceName
    _priceName = [UILabel new];
    
    _priceName.text = @"¥";
    _priceName.textColor = [UIColor colorWithRed:0.894 green:0.275 blue:0.180 alpha:1.000];
    [_myImageView addSubview:_priceName];
    
    [_priceName mas_makeConstraints:^(MASConstraintMaker *make) {
       make.size.mas_equalTo(CGSizeMake(10, 30));
       make.right.mas_equalTo(-70);
     make.top.mas_equalTo(_userImageView.mas_bottom).offset(60);
        
    }];
    
    
    
#pragma mark /**<价格*/
    _price = [UILabel new];
    _price.adjustsFontSizeToFitWidth = YES;
    [_myImageView addSubview:_price];
//    _price.backgroundColor = [UIColor orangeColor];
    [_price mas_makeConstraints:^(MASConstraintMaker *make) {
        //make.size.mas_equalTo(CGSizeMake(80, 100));
        make.height.mas_equalTo(50);
        make.left.mas_equalTo(_priceName.mas_right).offset(0);
        make.top.mas_equalTo(_data.mas_bottom).offset(5);
        
    }];
    _price.textColor = [UIColor colorWithRed:0.882 green:0.200 blue:0.063 alpha:1.000];
    [_price setFont:[UIFont fontWithName:@"Lao Sangam MN" size:30.f]];
    

    
    _lastLael = nil;
    
    

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(70, 25);
    layout.sectionInset = UIEdgeInsetsMake(4, 4, 4, 4);
    
     layout.minimumInteritemSpacing = 2;
    layout.minimumLineSpacing = 3;
    //设置滚动方向
   // layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
 
    // CGRectMake(10, 310, 230 * AUTO_WIDTH, 54)
    _collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    
    _collection.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_collection];
    [_collection registerClass:[YHYHunterCitySubCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
    [_collection mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_data).offset(-5);
        make.top.mas_equalTo(_data.mas_bottom).offset(15);
        make.width.mas_equalTo(@(230));
        make.height.mas_equalTo(@34);
    }];
    
    

     [_collection reloadData];
    
}
- (void)getYHYHunterCityModelOfArray:(NSMutableArray *)array{
    _myArrayOfLable = array;

    
    // 如果数组个数大于2个, 更新约束
    
    // 如果是4s
    if ([[UIScreen mainScreen] bounds].size.height < 500)
    {
        if (array.count > 2) {
            [_collection mas_updateConstraints:^(MASConstraintMaker *make) {

                make.height.mas_equalTo(@(34 + 25));
                make.top.mas_equalTo(_data.mas_bottom).offset(2);
                
            }];
        } else {
            [_collection mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(@(230 - 70 - 4));
                
                make.top.mas_equalTo(_data.mas_bottom).offset(15);
                make.height.mas_equalTo(@35);
                
            }];
        }
    }
    
    [_collection reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _myArrayOfLable.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellCollection = @"cell";
    YHYHunterCitySubCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellCollection forIndexPath:indexPath];
    
    
    NSString *str = _myArrayOfLable[indexPath.row];
    cell.myStr = str;

    return cell;
    
}



//#pragma mark tab_list
//
//    _lastLael = nil;
//    
//
//}
//- (UILabel *)newLabel
//{
//    if (nil == _newLabel) {
//        UILabel *newLabel = [UILabel new];
//        // 裁掉边框
//        //        newLabel.layer.masksToBounds = YES;
//        
//        newLabel.layer.borderWidth = 1.f;
//        newLabel.layer.borderColor = [UIColor colorWithWhite:0.8126 alpha:1.0].CGColor;
//        // 设置半径
//        newLabel.layer.cornerRadius = 3;
//        
//        _newLabel.textColor = [UIColor colorWithWhite:0.791 alpha:1.000];////
//        [_newLabel setFont:[UIFont fontWithName:@"Lao Sangam MN" size:15]];
//        //文字居中
//        _newLabel.textAlignment = NSTextAlignmentCenter;
//        
//        
//        [self.contentView addSubview:newLabel];
//        
//        [newLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            
//            make.size.mas_equalTo(CGSizeMake(62, 24));
//            
//            make.top.mas_equalTo(_data.mas_bottom).offset(10);
//            if (_lastLael) {
//                make.left.mas_equalTo(_lastLael.mas_right).offset(13);
//            }
//            else{
//                make.left.mas_equalTo(@13);
//            }
//            
//        }];
//
//    }
//    return _newLabel;
//}
- (void)setMyYHYHunterCityModel:(YHYHunterCityModel *)myYHYHunterCityModel{
    
    [_pageImageView sd_setImageWithURL:[NSURL URLWithString:myYHYHunterCityModel.title_page] placeholderImage:PICHOLDEER];
    
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:myYHYHunterCityModel.userAvatar_l] placeholderImage:PICHOLDEER];
    
    _title.text = myYHYHunterCityModel.title;
    
       _data.text = [NSString stringWithFormat:@"%@  ·  ",myYHYHunterCityModel.date_str];
    
    _price.text = [NSString stringWithFormat:@"%@",myYHYHunterCityModel.price];
    
    _address.text = [NSString stringWithFormat:@"%@",myYHYHunterCityModel.address];
    
    _like_count.text = [NSString stringWithFormat:@" %ld人喜欢",(long)myYHYHunterCityModel.like_count];
    
    if (myYHYHunterCityModel.address.length == 0) {
        
        [_like_count mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.height.mas_equalTo(_data);
            make.left.mas_equalTo(_data.mas_right).offset(1);
            make.width.mas_equalTo(100);
            
        }];
        
        _like_count.text = [NSString stringWithFormat:@"%ld人喜欢",(long)myYHYHunterCityModel.like_count];
    }
    else{
        _address.text = [_address.text stringByAppendingString:@"  ·"];
       
        if (myYHYHunterCityModel.address.length >= self.bounds.size.width/100) {
            
//            NSLog(@">13%@",myYHYHunterCityModel.address);
            //>13给出固定宽度＋
            
        
            [_address mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.left.mas_equalTo(_data.mas_right).offset(0);
                make.height.top.mas_equalTo(_data);
                make.width.mas_equalTo(@(SCREEN_WIDTH * 0.3f));
            }];
            
            [_like_count mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(_address.mas_right).offset(1);
                make.top.height.mas_equalTo(_data);
                
            }];
            

            
            
        }
        else{
         
            [_address mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(_data.mas_right).offset(0);
                make.top.height.mas_equalTo(_data);
                
                
            }];
            [_like_count mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(_address.mas_right).offset(1);
                make.top.height.mas_equalTo(_data);
                
            }];
            
            
        }
        
    }
    
    [self getYHYHunterCityModelOfArray:myYHYHunterCityModel.tab_list];
   

    
    
   
}

- (void)awakeFromNib {
    // Initialization code
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
